package de.gwtprime.client.widgets;

import java.util.ArrayList;
import java.util.List;

import org.moxieapps.gwt.highcharts.client.Axis;
import org.moxieapps.gwt.highcharts.client.AxisTitle;
import org.moxieapps.gwt.highcharts.client.Credits;
import org.moxieapps.gwt.highcharts.client.Series;
import org.moxieapps.gwt.highcharts.client.Series.Type;
import org.moxieapps.gwt.highcharts.client.StockChart;

import com.google.gwt.i18n.client.DateTimeFormat;

import de.gwtprime.shared.Day;
import de.gwtprime.shared.Stock;

public class MyStockChart {


	public static void main(String[] args) {
		Stock stock = new Stock();

		List<Day> days = new ArrayList<Day>();


		days.add(new Day(DateTimeFormat.getFormat("yyyy.MM.dd").parse("2014.01.01"), 200d, 300d));
		days.add(new Day(DateTimeFormat.getFormat("yyyy.MM.dd").parse("2014.01.01"), 310d, 320d));
		days.add(new Day(DateTimeFormat.getFormat("yyyy.MM.dd").parse("2014.01.01"), 320d, 400d));
		stock.setDays(days);
	}

	/**
	 * Create highstock chart
	 * @param stock
	 */
	public static StockChart createStockChart(Stock stock){


		final StockChart stockChart = new StockChart()
		.setChartTitleText(stock.getDisplayName());

//		stockChart.setRangeSelector(new RangeSelector()
//		.setButtons(
//				new RangeSelector.Button()
//				.setType(RangeSelector.ButtonType.MINUTE)
//				.setCount(1)
//				.setText("6aah"),
//				new RangeSelector.Button()
//				.setType(RangeSelector.ButtonType.DAY)
//				.setCount(1)
//				.setText("1d"),
//				new RangeSelector.Button()
//				.setType(RangeSelector.ButtonType.DAY)
//				.setCount(2)
//				.setText("2d")
//				)
//				.setSelected(1));
//				.setInputEnabled(false)
//				.setButtonSpacing(10));
                //Number[][] numbers2 = new Number[stock.getDays().size()][2];
               // Number[][] keltnerkanel_unten = new Number[stock.getDays().size()][2];

		stockChart.setOption("rangeSelector/enabled", false);
		//stockChart.setOption("rangeSelector/selected", 2);
		//stockChart.setOption("rangeSelector/buttons/", "[{type: 'month',count: 1,	text: '1m'}]");
//		stockChart.setOption("rangeSelector/buttons/count", 1);
//		stockChart.setOption("rangeSelector/buttons/text", "abc");
		Number[][] numbers = new Number[stock.getDays().size()][2];
		//Number[][] numbers2 = new Number[stock.getDays().size()][2];
		// Number[][] keltnerkanel_unten = new Number[stock.getDays().size()][2];

		int i=0;
		for(Day day : stock.getDays()){
                        numbers[i] = new Number[]{day.getDate().getTime()+1000*60*60,Math.round(day.getClosingPrice())};
                       // numbers2[i] = new Number[]{day.getDate().getTime(),Math.round(day.getKeltnerKanalOben())};
                       // keltnerkanel_unten[i] = new Number[]{day.getDate().getTime(),Math.round(day.getKeltnerKanalUnten())};
                       // i++;

			//numbers[i] = new Number[]{day.getDate().getTime()+1000*60*60,Math.round(day.getClosingPrice())};
			// numbers2[i] = new Number[]{day.getDate().getTime(),Math.round(day.getKeltnerKanalOben())};
			// keltnerkanel_unten[i] = new Number[]{day.getDate().getTime(),Math.round(day.getKeltnerKanalUnten())};
			i++;

		}

		Series ohlc = stockChart.createSeries()
				.setName(stock.getDisplayName())
				.setType(Type.LINE)

				.setPoints(numbers);

		stockChart.addSeries(ohlc);

		//                Series keltnerKanal = stockChart.createSeries()
		//                                .setName("changeMe")
		//                                .setType(Type.LINE)
		//                                .setPoints(numbers2);
		//                stockChart.addSeries(keltnerKanal);
		//
		//                Series keltnerKanalUnten = stockChart.createSeries()
		//                                .setName("changeMe")
		//                                .setType(Type.LINE)
		//                                
		//                                .setPoints(keltnerkanel_unten);
		//
		//                stockChart.addSeries(keltnerKanalUnten);
               stockChart.getXAxis().setType(Axis.Type.DATE_TIME).setOption("ordinal", true);
                stockChart.getYAxis()
                .setAxisTitle(new AxisTitle()  
                .setText("LTC price")); 

		//stockChart.getXAxis().setType(Axis.Type.DATE_TIME).setOption("ordinal", true);
		stockChart.getYAxis()
		.setAxisTitle(new AxisTitle()  
		.setText("LTC price")); 

		// add Flags

		//                for(int j=0;j<stock.getTradeResult().getPositions().size();j++){
		//                        TradingPosition position = stock.getTradeResult().getPositions().get(j);
		//                        addFlagsToSeries(stockChart, ohlc,  position.getOpenDate(), "changeMe", "changeMe", position.getOpenValue(), position);
		//                        addFlagsToSeries(stockChart, ohlc,  position.getCloseDate(), "changeMe", "changeMe", position.getCloseValue(), position);
		//                }

		//                Chart barChart = PositionColumChart.getColumnChart(stock); 

		// disable high charts branding
		stockChart.setCredits(new Credits().setEnabled(false));

		return stockChart;

	}

	/**
	 * Add flags to this chart
	 * @param stockChart Chart to add flags to
	 * @param keltnerKanal Series to add flags to
	 * @param date Where to add flag on x-axis?
	 * @param title Title of the flag
	 * @param description Description of the flag
	 * @param value Value on y-axis
	 */
//	private static void addFlagsToSeries(StockChart stockChart, Series keltnerKanal, Date date, String title, String description, Double value, TradingPosition position) {
//		stockChart.addSeries(stockChart.createSeries()
//				.setOption("type", "flags")
//				.setOption("onSeries", keltnerKanal.getOptions().get("id"))
//				.setOption("shape", "circlepin")
//				.setOption("width", 32) // with of the plags
//				.setOption("takeOrdinalPosition", false)
//				.setOption("ordinal", true)
//
//				.setPoints(new Point[]{
//						new Point(date.getTime(),value)
//						.setOption("title", title).setName(position.getType()+" Position")
//						.setOption("text", description),
//
//				})
//				);
//	}

}
